﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace az
{
    class LargestFirst
    {
        private List<Vertex> listVertex;
        private bool[] colorNeighboursTab;
        private int[] colorUses;

        public void LargestFirstAlgoritm(Graph graph, bool isMaxNr, int maxNr)
        {
            listVertex = graph.listVertex;
            if (listVertex.Count > 0)
            {
                listVertex.Sort();
                colorNeighboursTab = new bool[graph.countVertex+1];
                if (isMaxNr)
                {
                    colorUses = new int[graph.countVertex + 1];
                    for (int index = 0; index < colorUses.Length; index++)
                        colorUses[index] = 0;
                    foreach (Vertex v in listVertex)
                    {
                        for (int index = 0; index < colorNeighboursTab.Length; index++)
                            colorNeighboursTab[index] = false;
                        foreach (var vl in v.listVertexNeighbor)
                            if (vl.numberColor > 0)
                                colorNeighboursTab[vl.numberColor] = true;
                        AddColorWithMaxNr(v, 1, maxNr);
                    }
                }
                else
                {
                    foreach (Vertex v in listVertex)
                    {
                        for (int index = 0; index < colorNeighboursTab.Length; index++)
                            colorNeighboursTab[index] = false;
                        foreach (var vl in v.listVertexNeighbor)
                            if (vl.numberColor > 0)
                                colorNeighboursTab[vl.numberColor] = true;
                        AddColor(v, 1);
                    }
                }
                graph.isColour = true;
            }
        }

        private void AddColor(Vertex v, int l)
        {
            int k;
            k = l + 1;
            if(colorNeighboursTab[l])
                AddColor(v,k);
            else
            {
                v.numberColor = l;
                Console.WriteLine(v.number + " stopien " + v.countVertexNeighbour + " kolor " + v.numberColor);
            }
        }

        private void AddColorWithMaxNr(Vertex v, int l, int max)
        {
            int k;
            k = l + 1;
            if (colorNeighboursTab[l] || colorUses[l] == max)
                AddColorWithMaxNr(v, k, max);
            else
            {
                v.numberColor = l;
                colorUses[l]++;
                Console.WriteLine(v.number + " stopien " + v.countVertexNeighbour + " kolor " + v.numberColor);
            }
        }
    }
}
